﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.SqlClient;
using System.Text;
using prototype123.models;
using prototype123.controllers;

namespace prototype123.data_access
{
    //This class handles all data access between the application and the
    //user table in the database.
    class UserDAO
    {
        private SqlConnection db_connection;// = prototype123.Program.connect.GetConnection();

        public UserDAO(SqlConnection connection)
        {
            db_connection = connection;
        }

        /// <precondition>System needs to validate login information</precondition>
        /// <summary>
        /// Attempts to authenticate the user's login with the database
        /// </summary>
        /// <param name="name">Login username of the user</param>
        /// <param name="pass">Login password of the user</param>
        /// <returns>User object - passes back the user's information after successful login</returns>
        /// <postconditions>System now knows whether or not login succeeded.</postconditoins>
        public User Login(string name, string pass)
        {
            User usr = new User();
            SqlCommand password;
            string passwordResult;


            password = new SqlCommand("SELECT EDU_ID, PASSWORD FROM USERS WHERE USERNAME = '" + name + "'", db_connection);
            SqlDataReader reader = password.ExecuteReader();
            reader.Read();
            int IDResult = Convert.ToInt32(reader[0].ToString());
            passwordResult = reader[1].ToString();
            reader.Close();
            
            usr.Username = name;
            usr.Password = passwordResult;
            usr.EducatorID = IDResult;
            return usr;
        }
    }
}
